package com.kwz.common.ui.components.initializers;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.Icon;

import com.kwz.common.CommonException;
import com.kwz.common.Context;
import com.kwz.common.IContext;
import com.kwz.common.designpatterns.ExceptionManager;
import com.kwz.common.designpatterns.factory.BadContextException;
import com.kwz.common.ui.components.IUiComponentContextInitializer;
import com.kwz.common.ui.components.IUiComponentInitializer;

/**
 * <p>
 * 
 * </p> 
 *
 * @author mbi
 */
public class UiDefaultJButtonInitializer extends UiDefaultComponentInitializer implements IrUiInitializers, IUiComponentInitializer {

    /**
     * 
     */
    public UiDefaultJButtonInitializer() {
        super();

    }

    /* (non-Javadoc)
     * @see com.kwz.common.ui.components.IUiComponentInitializer#init(java.awt.Component, com.kwz.common.IContext)
     */
    public void init(Component component, IContext ctx) throws BadContextException {
        try{
        	AbstractButton button = (AbstractButton) component;
	        //Initialisation de l'icone
	        if(ctx.containsKey(UI_INIT_ICON)){
	            button.setIcon((Icon)ctx.getObjectProperty(UI_INIT_ICON));	            
	        }        
	        
	        //Initialisation du texte
	        if(ctx.containsKey(UI_INIT_LABEL)){
	            button.setText(ctx.getStringProperty(UI_INIT_LABEL));	            
	        }        
	        
	        //Initialisation de l'action
	        if(ctx.containsKey(UI_INIT_ACTION)){
	            button.setAction((Action)ctx.getObjectProperty(UI_INIT_ACTION));
	        }

	        //Initialisation de la couleur avant plan
	        if(ctx.containsKey(UI_INIT_FOREGROUND_COLOR)){
	            button.setForeground((Color)ctx.getObjectProperty(UI_INIT_FOREGROUND_COLOR));
	        }

	        //Initialisation de l'alignement horizontal
	        if(ctx.containsKey(UI_INIT_LABEL_ALIGNMENT)){
	            button.setHorizontalAlignment(ctx.getIntProperty(UI_INIT_LABEL_ALIGNMENT));
	        }

	        //Initialisation de la police
	        if(ctx.containsKey(UI_INIT_FONT)){
	            button.setFont((Font)ctx.getObjectProperty(UI_INIT_FONT));
	        }
	        
        }catch(CommonException ce){
            ExceptionManager.processExceptionNotThrow(this, ce);
        }

    }
    
    /* (non-Javadoc)
     * @see com.kwz.common.ui.components.IUiComponentInitializer#getDefaultInitializerContext()
     */
    public IContext getDefaultInitializerContext() {
        return getDefaultInitializerContext(getContextInitializer());
    }
    
    /* (non-Javadoc)
     * @see com.kwz.common.ui.components.IUiComponentInitializer#getDefaultInitializerContext(com.kwz.common.ui.components.IUiComponentContextInitializer)
     */
    public IContext getDefaultInitializerContext(IUiComponentContextInitializer contextInitializer) {
        IContext context = new Context();
        context.putProperty(UI_INIT_LABEL, contextInitializer.getContextValueFor(UI_JBUTTON, UI_INIT_LABEL));
        context.putProperty(UI_INIT_LABEL_ALIGNMENT,contextInitializer.getContextValueFor(UI_JBUTTON, UI_INIT_LABEL_ALIGNMENT));        
        return context;
    }

}
